package cn.huugh.number.controller;

import cn.huugh.number.NumberApplication;
import cn.huugh.number.cn.hugh.netutil.DataSource;
import cn.huugh.number.service.MyPoint;
import cn.huugh.number.service.NumberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@Controller
public class NumberController {
    @RequestMapping(value = "number")
    public void analyzeNumber(HttpServletRequest request, HttpServletResponse response, @RequestBody List<MyPoint> data) throws IOException {
        // Init Processor
        int x,y;
        MyPoint pMax = data.get(0);
        x=pMax.x;
        y=pMax.y;
        data.remove(0);
        System.out.println(x+" "+y);
        System.out.println(data.toString());
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        NumberService service = (NumberService) NumberApplication.context.getBean("NumberService");
        // Init Processor done! *

        // Logic code
        service.sendImg2Pytorch(service.buildImg(data,new MyPoint(x,y)));
        // Logic code done! *
        DataSource thisDataSource = new DataSource();
        NumberApplication.dataSources.add(thisDataSource);
        while(thisDataSource.data==null){ }  // 阻塞这一步，等待模型返回数据
        out.print(thisDataSource.data);
        NumberApplication.dataSources.remove(0);
        //System.out.println("22222");
        // return data to Android View

    }
}
